using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ebookreader
{
    public class VerticalScrollableSelector : Menu
    {

        SpriteFont font;
        public string value;
        public int selected_index;
        string[] values;
        
        int PADDING = 5;
        State currentState;
        List<BoxedLabelScrollableButton> buttons = new List<BoxedLabelScrollableButton>();


        public VerticalScrollableSelector(Game game, SpriteFont font, string[] values, int selected_index, Rectangle area, IInputManager input_manager) 
            : base(game, area, input_manager)
        {
            this.font = font;
            this.values = values;
            this.selected_index = selected_index;
           
        }

        public override void Initialize()
        {
            //input_manager = Game.Services.GetService(typeof(IInputManager)) as IInputManager;
            sprite_batch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            currentState = Game.Services.GetService(typeof(State)) as State;
            enableVerticalDrag = false;
            enableHorizzontalDrag = true;
            DrawOrder = 8;
            base.Initialize();
        }

        Texture2D ui_lang_box, mask;
        protected override void LoadContent()
        {
            //List<string> langs = new List<string>(){"","en","it"};
             ui_lang_box = Game.Content.Load<Texture2D>("Definitivo/lang_item");
             mask = Game.Content.Load<Texture2D>("Definitivo/lang_mask");
            //area = new Rectangle((int)position.X, (int)position.Y-PADDING, ui_lang_box.Width + 2 * PADDING, ui_lang_box.Height + 4 * PADDING);
            for (int i = 0; i < values.Length; i++)
            {
                    var lang = new BoxedLabelScrollableButton(sprite_batch,
                        ui_lang_box,new Vector2(area.Left + i * (ui_lang_box.Width+PADDING), area.Top)
                        , font, values[i], Color.Black, currentState.options);
                    lang.OnPress += new Press((me) => { });
                    entries.Add(lang);
                    buttons.Add(lang);
                    lang.splitLabel();
                }

            offsetPosition.X = -(selected_index-1) * (ui_lang_box.Width + PADDING);

            base.LoadContent();
        }


        public override void Update(GameTime gameTime)
        {
            if (!dragging)
            {
                offsetPosition.X += forceX;
                if (Math.Abs(forceX) >= 1f)
                    forceX = MathHelper.Lerp(forceX, 0.0f,
                        (float)gameTime.ElapsedGameTime.TotalSeconds * (float)Math.Max(60 / Math.Abs(forceX), 4f));
                else
                    forceX = 0.0f;
            }

            var fixPosition = 10000.0f;
            var firstIndex = 0;
            int i = 0;
            foreach (var m in buttons)
            {
                //if (bookOrder[i] > 0 && bookOrder[i] > books_buttons.Count)
                //    continue;
                if (Math.Abs(fixPosition) > Math.Abs((m.box_position.X + offsetPosition.X) -
                    (area.Center.X - ui_lang_box.Width / 2 + PADDING)))
                {
                    fixPosition = (m.box_position.X + offsetPosition.X) -
                    (area.Center.X - ui_lang_box.Width / 2 + PADDING);
                    firstIndex = i;
                }
                m.active = true;
                //if (m.box_position.X + offsetPosition.X < LIST_MARGIN - 10 ||
                //    m.box_position.X + offsetPosition.X > GraphicsDevice.Viewport.Height - title_box_heigth)
                //    m.active = false;
                i++;
                m.color = Color.White;
                m.box_color = Color.White;
            }

            buttons[firstIndex].box_color = Color.White * 0.4f;
            buttons[firstIndex].color = Color.Black;
            value = values[firstIndex];
            selected_index = firstIndex;
            if (!dragging)
            {
                if (fixPosition != 0.0f)
                    offsetPosition.X =
                        MathHelper.Lerp(offsetPosition.X, offsetPosition.X - fixPosition,
                        (float)gameTime.ElapsedGameTime.TotalSeconds * 5);
            }






            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {


            
            base.Draw(gameTime);
            sprite_batch.Begin();
            sprite_batch.Draw(mask, new Vector2(225, 202), Color.White);
            sprite_batch.End();
        }

    }
}



